<?php
class BuyAccountService {
	//以下三个参数对应的是积分记录 而不是订单
	private $_chargeType = array (0 => "充值", 1 => "消费", 2 => "赠送" );
	private $_payment = array ("收入" => 0, "支出" => 1 );
	private $_status = array (0 => "等待审核", 1 => "成功", - 1 => "关闭" );
	private $_num = 40;
	
	public function addAccount($user, $order, $status) {
		$params = array ('uid' => $user ['uid'], 'money' => $order ['money'], 'remark' => $order ['remark'], 'status' => $status, 'orderno' => $order ['orderno'], 'username' => $user ['real_name'], 'payment' => $order ['payment'], 'chargetypes' => $order ['chargetypes'], 'add_time' => time () );
		return BuyAccountModel::instance ()->addAccount ( $params );
	}
	
	public function getAccountByUid($pageid, $uid) {
		$total = BuyAccountModel::instance ()->countAcountByUid ( $uid );
		$page = $this->page ( $total, $pageid, $this->_num );
		$account = BuyAccountModel::instance ()->getAccountByUid ( $page ['start'], $this->_num, $uid );
		
		foreach ( $account as $k => $v ) {
			$account [$k] ['chargetypes'] = $this->_chargeType [$v ['chargetypes']];
			$account [$k] ['status'] = $this->_status [$v ['status']];
		}
		return array ('page' => $page, 'account' => $account );
	}
	
	public function getAccountByStatus($pageid, $status) {
		if (empty ( $status )) {
			$status = "all";
		}
		
		$total = BuyAccountModel::instance ()->countAcountByStatus ( $status );
		$page = $this->page ( $total, $pageid, $this->_num );
		$account = BuyAccountModel::instance ()->getAccountByStatus ( $page ['start'], $this->_num, $status );
		
		foreach ( $account as $k => $v ) {
			$account [$k] ['chargetypes'] = $this->_chargeType [$v ['chargetypes']];
			$account [$k] ['status'] = $this->_status [$v ['status']];
		}
		return array ('page' => $page, 'account' => $account, 'status' => $this->_status );
	}
	
	public function setAccountBySNO($status, $orderno) {
		return BuyAccountModel::instance ()->setAccountStatusBySNO ( $status, $orderno );
	}
	
    public function setAccountMoneyBySNO($money, $orderno) {
		return BuyAccountModel::instance ()->setAccountMoneyBySNO ( $money, $orderno );
	}
		
	public function removeAccountBySNO($orderno) {
		return BuyAccountModel::instance ()->removeAccountBySNO ( $orderno );
	}
	/**
	 * 分页
	 *
	 * @return void
	 */
	private function page($total, $pageid, $num) {
		$pageid = isset ( $pageid ) ? $pageid : 1;
		
		$start = ($pageid - 1) * $num;
		$pagenum = ceil ( $total / $num );
		/*修正分类不包含内容 显示404错误*/
		$pagenum = $pagenum == 0 ? 1 : $pagenum;
		/*如果超过了分类页数 404错误*/
		
		if ($pageid > $pagenum) {
			return false;
		}
		
		return array ('start' => $start, 'num' => $num, 'current' => $pageid, 'page' => $pagenum );
	}

}